package shun.demo.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;
import shun.demo.R;

/**
 * Created by chen on 3/5/14.
 */
public class CameraActivity extends Activity implements View.OnClickListener {

    public static final int CAMERA_REQUEST_CODE = 1000;
    public static final int VIDEO_REQUEST_CODE = 1001;
    private ImageView mImageView;
    private VideoView mVideoView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        mImageView = (ImageView) findViewById(R.id.image);
        mVideoView = (VideoView) findViewById(R.id.video_view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.camera:
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
                break;

            case R.id.video:
                Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(videoIntent, VIDEO_REQUEST_CODE);
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle extra = data.getExtras();
            Bitmap bitmap = (Bitmap) extra.get("data");
            mImageView.setImageBitmap(bitmap);
        } else if (requestCode == VIDEO_REQUEST_CODE && resultCode == RESULT_OK) {
            mVideoView.setVideoURI(data.getData());
            mVideoView.start();
        }
    }
}